package com.nyist.diseasepreventionserver.controller;

import com.nyist.diseasepreventionserver.VO.UserHealthVO;
import com.nyist.diseasepreventionserver.VO.UserListBeforeVO;
import com.nyist.diseasepreventionserver.VO.UserListVO;
import com.nyist.diseasepreventionserver.common.ResponseCode;
import com.nyist.diseasepreventionserver.common.ServerResponse;
import com.nyist.diseasepreventionserver.converter.UserDTOPage2UserListBeforeVO;
import com.nyist.diseasepreventionserver.converter.UserDTOPage2UserListVOConverter;
import com.nyist.diseasepreventionserver.dto.UserDTO;
import com.nyist.diseasepreventionserver.entity.HealthDaily;
import com.nyist.diseasepreventionserver.entity.User;
import com.nyist.diseasepreventionserver.service.UserService;
import com.nyist.diseasepreventionserver.util.SpringUtil;
import com.nyist.diseasepreventionserver.util.UserHealthUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/admin/user")
@Slf4j
public class AdminController {

    //管理员查看所有用户
    @GetMapping("/list")
    public ServerResponse list( Integer page, Integer size){
        UserService userService = SpringUtil.getBean(UserService.class);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        Page<UserDTO> userDTOPage = userService.findAll(pageRequest);
        UserListBeforeVO result = UserDTOPage2UserListBeforeVO.convert(userDTOPage);
        return ServerResponse.createBySuccess(result);
    }
    //管理员查看用户健康日报/搜索
    @GetMapping("/look")
    public ServerResponse adminLook(Integer userSid){
        UserService userService = SpringUtil.getBean(UserService.class);
        UserDTO userDTO = userService.findOne(userSid);
        if(userDTO.getUserSid() == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ID_no_exists.getCode(),
                    ResponseCode.ID_no_exists.getDesc());
        }else if (userDTO.getHealthDaily() == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.healthDaily_no_exists.getCode(),
                    ResponseCode.healthDaily_no_exists.getDesc());
        }
        UserHealthUtil userHealthUtil = new UserHealthUtil();
        UserHealthVO userHealthVO = userHealthUtil.toVO(userDTO);
        return ServerResponse.createBySuccess(userHealthVO);
    }

}
